package com.lyp.activitys;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.blankj.utilcode.util.ToastUtils;
import com.lyp.jsonbean.MYesyuyueBean;
import com.lyp.jsonbean.YuyueBean;
import com.tjl.yangxixis.R;
import com.tjl.yangxixis.api.YangxixiApi;

import java.io.IOException;

import cn.qqtheme.framework.picker.DateTimePicker;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

//预约(已预约)
public class YuyueYiActivity extends Activity implements View.OnClickListener{
    private ImageView mBack;
    private EditText mEtBz;
    private Button mBtnTime,mCommit;
    private TextView mTvTime;
    private MYesyuyueBean.DataBean mBean;
    private String mNote;
    private String mTime;
    private LinearLayout mLLcommit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_yuyue);
        initView();
    }

    private void initView() {
        mBean = (MYesyuyueBean.DataBean) getIntent().getExtras().get("databean");
        mBack = (ImageView) findViewById(R.id.yuyue_back);
        mEtBz = (EditText) findViewById(R.id.et_bz);
        mBtnTime = (Button) findViewById(R.id.btn_time);
        mCommit = (Button) findViewById(R.id.btn_order_confirm);
        mTvTime = (TextView) findViewById(R.id.tv_time);
        mLLcommit = (LinearLayout) findViewById(R.id.ll_commit);

        mBack.setOnClickListener(this);
        mBtnTime.setOnClickListener(this);
        mCommit.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.yuyue_back:
                finish();
                break;
            case R.id.btn_time:
                onYearMonthDayTimePicker(v);
                break;
            case R.id.btn_order_confirm:
                try {
                    mNote = mEtBz.getText().toString();
                    mTime = mTvTime.getText().toString();
                    yuyue(mBean.getO_id(),mNote,mTime);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                break;
        }
    }

    /**
     * 返回数据
     */
    private void returnData() {
        Intent intent = new Intent();
        intent.putExtra("bz", mNote);
        intent.putExtra("time", mTime);
        setResult(RESULT_OK, intent);
        finish();
    }

    public void onYearMonthDayTimePicker(View view) {
        DateTimePicker picker = new DateTimePicker(this, DateTimePicker.HOUR_24);
        picker.setDateRangeStart(2017, 1, 1);
        picker.setDateRangeEnd(2025, 11, 11);
        picker.setTimeRangeStart(1, 0);
        picker.setTimeRangeEnd(23, 30);
        picker.setOnDateTimePickListener(new DateTimePicker.OnYearMonthDayTimePickListener() {
            @Override
            public void onDateTimePicked(String year, String month, String day, String hour, String minute) {
                mTvTime.setText(year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + "00");
                mCommit.setVisibility(View.VISIBLE);
                mLLcommit.setVisibility(View.VISIBLE);
            }
        });
        picker.show();
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        // 返回数据
        returnData();
    }

    //预约今日
    public void yuyue(String o_id,String note,String datetime) throws IOException {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(YangxixiApi.APP_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        YangxixiApi api = retrofit.create(YangxixiApi.class);
        Call<YuyueBean> call = api.getYuyue(o_id,note,datetime);
        call.enqueue(new Callback<YuyueBean>() {
            @Override
            public void onResponse(Call<YuyueBean> call, final Response<YuyueBean> response) {
                if (response.body().getResult() == 1 ) {
                    YuyueYiActivity.this.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            new MaterialDialog.Builder(YuyueYiActivity.this)
                                    .content("恭喜你预约成功!")
                                    .positiveText("确定")
                                    .canceledOnTouchOutside(false)
                                    .cancelable(false)
                                    .onPositive(new MaterialDialog.SingleButtonCallback(){
                                        @Override
                                        public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                                            returnData();
                                        }
                                    })
                                    .show();
                        }
                    });
                }
                else {
                    ToastUtils.showShort(response.body().getMessage());
                }
            }
            @Override
            public void onFailure(Call<YuyueBean> call, Throwable t) {
                ToastUtils.showShort(t.getMessage());
            }
        });
    }
}